<template>
  <div class="ppt">
    <NavHeader title="萤星PPT" />
    <div class="ppt-content">
      <div class="content-left">
        <LeftSider
          :id="id"
          title="PPT"
          :list="chatlist"
          @transmit="getMessage"
          @del="del"
          @clear="clear"
        />
      </div>
      <div class="content-right">
        <div v-if="typePage == 0" class="content1">
          <div class="right-con">
            <div class="ppt-title">
              <span>AI一键生成PPT</span>
            </div>
            <div class="ppt-create__advantage">
              <div class="advantage-item">
                <div class="advantage-item__img">
                  <img src="@/assets/imgs/ppt-advantage-0.png" alt="" />
                </div>
                <div class="advantage-item__title">轻松一点内容自现</div>
                <div class="advantage-item__content">PPT创作本就如此简单</div>
              </div>
              <div class="advantage-item">
                <div class="advantage-item__img">
                  <img src="@/assets/imgs/ppt-advantage-1.png" alt="" />
                </div>
                <div class="advantage-item__title">自动生成PPT大纲</div>
                <div class="advantage-item__content">AI助力内容编排</div>
              </div>
              <div class="advantage-item">
                <div class="advantage-item__img">
                  <img src="@/assets/imgs/ppt-advantage-2.png" alt="" />
                </div>
                <div class="advantage-item__title">PPT格式文稿直出</div>
                <div class="advantage-item__content">
                  从标题到上台演示，5分钟内搞定
                </div>
              </div>
            </div>
            <div class="ppt-card">
              <div class="card-title">立即创建PPT</div>
              <div class="ppt-input">
                <div class="group">PPT主题</div>
                <el-input
                  v-model="subject"
                  placeholder="请输入PPT主题（5~40字）"
                ></el-input>
              </div>
              <div class="ppt-input">
                <div class="group">您的角色</div>
                <el-input
                  v-model="profession"
                  placeholder="描述你的角色，例如，我是一个行业专家"
                >
                </el-input>
              </div>
              <div class="ppt-input input-select">
                <div class="group">PPT语言</div>
                <el-select v-model="language" placeholder="请选择">
                  <el-option
                    v-for="item in options"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </div>
              <div class="ppt-btn" @click="checkTokens">提交生成PPT</div>
            </div>
          </div>
        </div>
        <div v-if="typePage == 1 && showIframe" class="content2">
          <iframe
            id="test"
            ref="iframe"
            height="1000"
            width="100%"
            frameborder="no"
            scrolling="no"
            :src="src"
          />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import NavHeader from "@/components/NavHeader";
import LeftSider from "@/components/LeftSider";
import { fetchData } from "@/utils";
import { createPpt } from "@/api/chat";
import { getChatHis, delChatHis } from "@/api/translate";
import { getUserToken } from "@/api/api";
import { saveAs } from "file-saver";
import MarkdownIt from "markdown-it";
export default {
  name: "PPT",
  components: {
    NavHeader,
    LeftSider,
  },
  data() {
    return {
      iframeUrl: "",
      id: 0,
      showIframe: false,
      subject: "",
      typePage: 0,
      language: "chinese",
      profession: "",
      cacheFilter: { type: "5" },
      chatlist: [],
      options: [
        {
          value: "chinese",
          label: "中文",
        },
        {
          value: "english",
          label: "英文",
        },
      ],
      value: "",
      isloading: false,
      timer: null,
    };
  },
  mounted() {
    this.getChatHisList();
  },
  destroyed() {
    clearInterval(this.timer);
  },
  methods: {
    createPpt() {
      this.isloading = true;
      fetchData(createPpt, {
        subject: this.subject,
        type: this.language,
        describe: this.profession,
      }).then(() => {
        this.getChatHisList(true);
        this.timer = setInterval(() => {
          this.getChatHisList(true);
        }, 3000);
      });
    },
    checkTokens() {
      fetchData(getUserToken, 3)
      .then((res) => {
        if(res) {
          this.createPpt()
        } else {
          this.$message.warning("您的token不足，请充值!");
        }
      });
    },
    getChatHisList(bol) {
      fetchData(
        getChatHis,
        {
          ...this.cacheFilter,
          pagesize: 20,
          pagenum: this.pagenum,
        },
        false
      ).then((res) => {
        this.chatlist = res?.list || [];
        this.pages = res.pages;
        if (bol) {
          console.log(this.chatlist[0]);
          let data = this.chatlist[0];
          this.typePage = 0;
          this.showIframe = false;
          this.repotrContent = data.backtext || "";
          this.reportTitle = data.fromtext;
          this.id = data.id;
          if (this.chatlist.length > 0 && this.chatlist[0].syncstatus == "01") {
            clearInterval(this.timer);
            this.isloading = false;
            this.src = process.env.VUE_APP_PPT_URL + this.chatlist[0].id;
            this.showIframe = true;
            this.typePage = 1;
            setTimeout(() => {
              document
                .getElementById("test")
                .contentWindow.location.reload(true);
            }, 200);
          }
        }
      });
    },
    del(data) {
      console.log(data);
      fetchData(delChatHis, { id: data.id }).then(() => {
        this.$message.success("删除成功!");
        this.pagenum = 1;
        this.getChatHisList();
        this.clear();
      });
    },
    clear() {
      if (this.isloading) return;
      if (this.id == 0) {
        this.$message.warning("当前已是新PPT!");
      } else {
        this.id = 0;
        this.repotrContent = "";
        this.reportTitle = "";
        this.typePage = "0";
      }
    },
    getMessage(data) {
      if (this.isloading) return;
      data.syncstatus = "01";
      this.typePage = 0;
      this.showIframe = false;
      this.repotrContent = data.backtext || "";
      this.reportTitle = data.fromtext;
      this.id = data.id;
      this.src = process.env.VUE_APP_PPT_URL + data.id;
      console.log(this.src);
      this.showIframe = true;
      this.typePage = 1;
      setTimeout(() => {
        document.getElementById("test").contentWindow.location.reload(true);
      }, 200);
    },
    download() {
      var md = new MarkdownIt();
      const convertedHtml = md.render(this.repotrContent);
      console.log(convertedHtml);
      const blob = new Blob([convertedHtml], {
        type: "application/vnd.ms-powerpoint;charset=utf-8",
      });
      saveAs(blob, `${this.reportTitle}.ppt`);
    },
  },
};
</script>

<style lang="less" scoped>
.ppt {
  width: 100%;
  height: 100vh;
  display: flex;
  flex-direction: column;
  .ppt-content {
    flex: 1;
    display: flex;
    overflow: hidden;
    .content-left {
      height: 100%;
    }
    .content-right {
      flex: 1;
      overflow: auto;
      .content1 {
        padding: 50px 0 40px;
        background: url("../../assets/imgs/ppt-bg.png") no-repeat;
        background-size: 100% 100%;
        box-sizing: border-box;
        .right-con {
          width: 1200px;
          margin: 0 auto;
          .ppt-title {
            margin-top: 87px;
            text-align: center;
            color: #6c88fa;
            font-size: 26px;
            font-weight: 600;
            span {
              position: relative;
              &::after {
                content: "";
                height: 10px;
                background: #6965ea;
                width: 100%;
                opacity: 0.12;
                position: absolute;
                left: 0;
                bottom: 0;
              }
            }
          }
          .ppt-create__advantage {
            display: flex;
            justify-content: center;
            justify-content: space-around;
            margin-top: 35px;
            padding: 0 120px;
            box-sizing: border-box;
            .advantage-item {
              text-align: center;
              .advantage-item__img {
                width: 160px;
                img {
                  width: 100%;
                }
              }
              .advantage-item__title {
                font-size: 13px;
                font-weight: 600;
                margin: 8px 0;
              }
              .advantage-item__content {
                font-size: 13px;
                color: #595959;
              }
            }
          }
          .ppt-card {
            margin: 80px auto 120px;
            width: 960px;
            height: 340px;
            background: #ffffff;
            box-shadow: 0 6px 16px 1px #0000000a;
            border-radius: 16px;
            padding: 0 95px;
            border: 1px solid transparent;
            box-sizing: border-box;
            .card-title {
              color: #141414;
              margin: 40px 0 24px;
              font-size: 16px;
              font-weight: 600;
              text-align: center;
            }
            .ppt-input {
              margin-bottom: 16px;
              display: flex;
              border: 1px solid #dcdfe6;
              border-radius: 8px;
              transition: border-color 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);
              &:hover {
                border-color: #c0c4cc;
              }
              &:focus-within {
                border-color: #409eff;
              }
              .group {
                line-height: 40px;
                width: 93px;
                text-align: center;
                box-sizing: border-box;
                position: relative;
                font-size: 14px;
                &::after {
                  content: " ";
                  width: 1px;
                  height: 22px;
                  position: absolute;
                  background-color: #e6e6e6;
                  top: 8px;
                  right: 0;
                }
              }
              /deep/ .el-input {
                flex: 1;
                .el-input__inner {
                  border: 0;
                  border-radius: 8px;
                  box-sizing: border-box;
                }
              }
            }
            .input-desc {
              margin-top: 28px;
              height: 110px;
              position: relative;
              /deep/ .el-textarea {
                flex: 1;
                .el-textarea__inner {
                  border: 0;
                  border-radius: 8px;
                  box-sizing: border-box;
                  padding: 10px 15px;
                }
              }
              .tip-btn {
                position: absolute;
                bottom: 10px;
                left: 16px;
                margin-top: 10px;
                padding: 0 14px;
                line-height: 28px;
                background: #f6f5fd;
                border-radius: 14px;
                color: #6c69cb;
                font-size: 12px;
                cursor: pointer;
              }
            }
            .input-select {
              margin-top: 16px;
              width: 293px;
            }
            .ppt-btn {
              width: 140px;
              height: 42px;
              line-height: 42px;
              border-radius: 8px;
              background: linear-gradient(219deg, #6468e7 0%, #8a55fe 100%);
              transition: all 0.3s ease-in-out;
              cursor: pointer;
              text-align: center;
              color: #fff;
              font-size: 14px;
              margin: 22px auto;
              &:hover {
                box-shadow: 2px 4px 10px 1px #00000029;
              }
            }
          }
        }
      }
      .content2 {
        background-color: #fff;
        width: 100%;
        margin: 0 auto;
        // padding: 30px 30px 80px;
        box-sizing: border-box;
        min-height: calc(100vh - 54px);
        position: relative;
        .title {
          text-align: center;
          font-size: 30px;
          font-weight: 400;
          color: #3f4654;
          line-height: 42px;
          margin-top: 130px;
        }
        .text {
          font-size: 14px;
          line-height: 28px;
          color: #141414;
          margin-top: 60px;
        }
        .pbtn {
          position: absolute;
          top: 60px;
          right: 40px;
          border: none;
          outline: none;
          width: 132px;
          height: 36px;
          line-height: 36px;
          border-radius: 6px;
          text-align: center;
          color: #fff;
          background-color: #6965ea;
          cursor: pointer;
          font-size: 13px;
        }
      }
    }
  }
}
</style>
